<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="结算状态">
        <el-select style="width: 130px;" placeholder="结算状态" clearable v-model="dataForm.status" @change="onChange">
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="item in settlementStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号">
        <el-select filterable style="width: 130px" v-model="dataForm.truckId" placeholder="车牌号" @change="onChange">
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="item in vehicelCodeList"
            :key="item.id"
            :label="item.licensePlate"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="收货单位">
        <!--        <el-input v-model="dataForm.companyName" placeholder="收货单位" clearable></el-input>-->
        <el-select filterable style="width: 100%" v-model="dataForm.companyId" placeholder="收货单位"  @change="onChange">
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="item in receiveCompanyList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="驾驶员">
        <el-input v-model="dataForm.driverName" placeholder="驾驶员姓名" @input="onChange()" clearable></el-input>
      </el-form-item>
      <el-form-item label="运输日期">
        <el-date-picker
          :editable="false"
          type="date"
          v-model="dataForm.startDate"
          value-format="yyyy-MM-dd"
          style="width:150px;"
          placeholder="运输起始日期">
        </el-date-picker>
        ~
        <el-date-picker
          :editable="false"
          type="date"
          v-model="dataForm.endDate"
          style="width:150px;"
          value-format="yyyy-MM-dd"
          placeholder="运输结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('tms:shipmentrecord:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('tms:shipmentrecord:import')" type="primary" @click="onImport()">导入</el-button>
        <!--        <el-button v-if="isAuth('tms:shipmentrecord:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <!--      <el-table-column-->
      <!--        type="selection"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        width="50">-->
      <!--      </el-table-column>-->
      <!--      <el-table-column-->
      <!--        prop="id"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="">-->
      <!--      </el-table-column>-->
      <el-table-column
        fixed
        width="150px"
        prop="shipmentDate"
        header-align="center"
        align="center"
        label="运输日期">
      </el-table-column>
      <el-table-column
        width="100px"
        prop="licensePlate"
        header-align="center"
        align="center"
        label="车牌号">
      </el-table-column>
      <el-table-column
        width="100px"
        prop="driverName"
        header-align="center"
        align="center"
        label="驾驶员">
      </el-table-column>
      <el-table-column
        width="150px"
        prop="companyName"
        header-align="center"
        align="center"
        label="收货单位">
      </el-table-column>
      <el-table-column
        width="150px"
        prop="sendStation"
        header-align="center"
        align="center"
        label="发出站">
      </el-table-column>
      <el-table-column
        prop="settlementStatus"
        header-align="center"
        align="center"
        label="结算状态">
        <template slot-scope="scope">
          {{ scope.row.settlementStatus == 0 ? '未结算' : '已结算' }}
        </template>
      </el-table-column>

      <el-table-column
        prop="receiveWeight"
        header-align="center"
        align="center"
        label="实收吨数">
      </el-table-column>
      <el-table-column
        prop="sendWeight"
        header-align="center"
        align="center"
        label="矿发吨数">
      </el-table-column>
      <el-table-column
        width="100px"
        prop="price"
        header-align="center"
        align="center"
        label="每吨运费">
      </el-table-column>
      <el-table-column
        width="100px"
        prop="shipmentAmount"
        header-align="center"
        align="center"
        label="总运费">
      </el-table-column>
      <el-table-column
        prop="shipmentCount"
        header-align="center"
        align="center"
        label="运输车次">
      </el-table-column>
      <el-table-column
        prop="salary"
        header-align="center"
        align="center"
        label="工资(元)">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        width="130px"-->
      <!--        prop="energyType"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="车辆类别">-->
      <!--        <template slot-scope="scope">-->
      <!--          {{scope.row.energyType == 1?'燃油车':'电动车'}}-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        width="130px"
        prop="fuelAmount"
        header-align="center"
        align="center"
        label="燃料费(油/电)">
      </el-table-column>
      <el-table-column
        width="130px"
        prop="prepayAmount"
        header-align="center"
        align="center"
        label="预支消费(元)">
      </el-table-column>
      <el-table-column
        width="130px"
        prop="otherAmount"
        header-align="center"
        align="center"
        label="其他费用(元)">
      </el-table-column>
      <el-table-column
        width="130px"
        prop="totalAmount"
        header-align="center"
        align="center"
        label="总支出费(元)">
      </el-table-column>

<!--      <el-table-column-->
<!--        prop="type"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="车辆所属">-->
<!--        <template slot-scope="scope">-->
<!--          {{scope.row.belong ==1?'私有车辆':'外部车辆'}}-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column
        width="170px"
        prop="createTime"
        header-align="center"
        align="center"
        label="录入时间">
      </el-table-column>
      <el-table-column
        prop="creator"
        header-align="center"
        align="center"
        label="录入人员">
      </el-table-column>
      <el-table-column
        width="200px"
        prop="note"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" v-if="isAuth('tms:shipmentrecord:save' )&& scope.row.settlementStatus == 0"
                     size="small" @click="copyRowData(scope.row)">拷贝
          </el-button>
          <el-button type="text" v-if="isAuth('tms:shipmentrecord:update')"
                     size="small" @click="addOrUpdateHandle(scope.row.id,scope.row.settlementStatus)">修改
          </el-button>
          <!--          <el-button type="text"  v-if="isAuth('tms:shipmentrecord:save')"  size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" :vehicelCodeList="vehicelCodeList" :receiveCompanyList="receiveCompanyList"
                   :driverList="driverList" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 导入 -->
    <import-sub v-if="importVisible" ref="importRef" @refreshDataList="getDataList"></import-sub>
  </div>
</template>

<script>
import ImportSub from './import-sub';
import AddOrUpdate from './shipmentrecord-add-or-update'
import Utils  from "@/utils/utils";
export default {
  data() {
    return {
      settlementStatusList: [{label: '已结算', value: 1}, {label: '未结算', value: 0}],
      dataForm: {
        truckId: '',
        driverName: '',
        startDate: '',
        endDate: '',
        companyName: '',
        companyId: '',
        status:''
      },
      dataList: [],
      vehicelCodeList: [],
      driverList: [],
      receiveCompanyList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      importVisible: false,
    }
  },
  components: {
    AddOrUpdate, ImportSub
  },
  activated() {
    this.getDataList()
    this.getVehicelCodeList()
    this.getReceiveCompanyList()
    this.getDriverList()
  },
  methods: {
    onChange(){
      this.pageIndex =1;
      this.getDataList();
    },
    copyRowData(data) {
      console.log(data);
      data.id = '';
      this.addOrUpdateHandle('', 0,'copy', data)
      // data.settlementStatus =0;
      // data.shipmentDate =''
      //this.dataFormSubmit(data)
    },
    // 表单提交
    dataFormSubmit(data) {
      let that = this;
      this.$http({
        url: this.$http.adornUrl(`/tms/shipmentrecord/save`),
        method: 'post',
        data: this.$http.adornData(data)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              that.getDataList();
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getDriverList() {
      this.$http({
        url: this.$http.adornUrl('/tms/driver/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': 1,
          'limit': 999
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.driverList = data.page.list

        }
      })
    },
    getReceiveCompanyList() {
      this.$http({
        url: this.$http.adornUrl('/tms/company/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': 1,
          'limit': 999
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.receiveCompanyList = data.page.list

        }
      })
    },
    getVehicelCodeList() {
      this.$http({
        url: this.$http.adornUrl('/tms/truck/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': 1,
          'limit': 999
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.vehicelCodeList = data.page.list

        }
      })
    },
    /**
     * 导入
     */
    onImport() {
      this.importVisible = true;
      this.$nextTick(() => {
        this.$refs.importRef.init();
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/tms/shipmentrecord/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'companyName': this.dataForm.companyName,
          'companyId': this.dataForm.companyId,
          truckId: this.dataForm.truckId,
          driverName: this.dataForm.driverName,
          startDate: this.dataForm.startDate,
          endDate: this.dataForm.endDate,
          settlementStatus:this.dataForm.status
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id,settlementStatus, type, data) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id,settlementStatus, type, data)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/tms/shipmentrecord/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
